/*
 * @(#)WorkflowPermissionSetupTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.taglib;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.RequestUtils;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.taglib.TagUtil;
import com.dcivision.framework.taglib.html.FormTag;
import com.dcivision.workflow.bean.WorkflowProgressAttachment;

/**
 * WorkflowPermissionSetupTag.java
 *
 * @author      Dick Xie
 * @company     DCIVision Limited
 * @creation date   2005/3/2
 * @version     $Revision: 1.18.4.8 $
 */
public class WorkflowAttachmentTag extends BodyTagSupport {

  public static final String REVISION = "$Revision: 1.18.4.8 $";
  protected Log log = LogFactory.getLog(this.getClass().getName());

  protected SessionContainer sessionContainer;
  protected String contextPath = null;
  protected String imageBase;
  protected String URL = null;
  protected String formName = null;
  protected String objectName = "attachmentDiv";

  protected int width = 400;
  protected int height = 25;
  protected String popW = "800";
  protected String popH = "650";
  protected String displayFileNames = null;
  protected String displayDmsFileNames = null;
  //added by nancy
  protected String fileNames = null;
  protected String dmsFileNames = null;
  
  protected List attachmentList = null;
  
  public String getFileNames() {
    return fileNames;
  }

  public void setFileNames(String fileNames) {
    this.fileNames = fileNames;
  }
  public String getDmsFileNames() {
    return dmsFileNames;
  }

  public void setDmsFileNames(String dmsFileNames) {
    this.dmsFileNames = dmsFileNames;
  }
  //end

  public WorkflowAttachmentTag(){
    super();
  }

  public String getFormName() {
    return formName;
  }

  public void setFormName(String formName) {
    this.formName = formName;
  }

  public String getURL() {
    return URL;
  }

  public void setURL(String url) {
    URL = url;
  }

  public String getObjectName() {
    return objectName;
  }

  public void setObjectName(String objectName) {
    this.objectName = objectName;
  }

  public SessionContainer getSessionContainer() {
    return sessionContainer;
  }

  public void setSessionContainer(SessionContainer sessionContainer) {
    this.sessionContainer = sessionContainer;
  }



  public String getContextPath() {
    return contextPath;
  }

  public void setContextPath(String contextPath) {
    this.contextPath = contextPath;
  }

  public String getPopH() {
    return popH;
  }

  public void setPopH(String popH) {
    this.popH = popH;
  }

  public String getPopW() {
    return popW;
  }

  public void setPopW(String popW) {
    this.popW = popW;
  }

  public int getHeight() {
    return height;
  }

  public void setHeight(int height) {
    this.height = height;
  }

  public int getWidth() {
    return width;
  }

  public void setWidth(int width) {
    this.width = width;
  }

  public String getDisplayDmsFileNames() {
    return displayDmsFileNames;
  }

  public void setDisplayDmsFileNames(String displayDmsFileNames) {
    this.displayDmsFileNames = displayDmsFileNames;
  }

  public String getDisplayFileNames() {
    return displayFileNames;
  }

  public void setDisplayFileNames(String displayFileNames) {
    this.displayFileNames = displayFileNames;
  }
   
  public List getAttachmentList() {
  	return attachmentList;
  }
  
  public void setAttachmentList(List attachmentList) {
  	this.attachmentList = attachmentList;
  }
  
  public int doStartTag() throws JspException {
    try {
      init();
      pageContext.getOut().print( this.getContent() );
    } catch (ApplicationException ae){
      log.error(ae.getMsgCode(), ae);
    } catch (Exception e) {
      log.fatal(e.getMessage(), e);
    } finally {
      this.release();
    }
    return(SKIP_BODY);
  }

  public int doEndTag() throws JspException {
    return EVAL_PAGE;
 }

  public void release() {
    super.release();
    this.displayFileNames = null;
    this.displayDmsFileNames = null;
    this.fileNames=null;
    this. dmsFileNames=null;
    this.contextPath = null;
    this.URL = null;
    this.formName = null;
    this.attachmentList = null;

  }

  protected boolean validateTag(){
    //return ( stepID!=null || (userActorList!=null&&filterBy!=null) );
    return true;
  }

  protected String getContent() throws ApplicationException {
    StringBuffer sb = new StringBuffer();
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);
    log.debug("the current formTag name is >>>>>>" + formTag.getBeanName());
    String navMode = null;

    try {
      navMode = (String)RequestUtils.lookup(pageContext, formTag.getBeanName(), "navMode", null);
      if (!GlobalConstant.NAV_MODE_NEW.equals(navMode)) {
      	if (Utility.isEmpty(this.displayFileNames)) {
      		displayFileNames = (String)RequestUtils.lookup(pageContext, formTag.getBeanName(), "displayFileNames", null);
      	}
      	if (Utility.isEmpty(this.fileNames)) {
      		fileNames = (String)RequestUtils.lookup(pageContext, formTag.getBeanName(), "fileNames", null);
      	}
      	if (Utility.isEmpty(this.displayDmsFileNames)) {
      		displayDmsFileNames = (String)RequestUtils.lookup(pageContext, formTag.getBeanName(), "displayDmsFileNames", null);
      	}
      	if (Utility.isEmpty(this.dmsFileNames)) {
      		dmsFileNames = (String)RequestUtils.lookup(pageContext, formTag.getBeanName(), "dmsFileNames", null);
      	}
      } else {
        log.debug("is new !!");
      }
       log.debug("The navMode is >>>>>" + navMode);
    } catch (JspException e) {
      // TODO Auto-generated catch block
      log.error(e, e);
    }
    JspWriter out = pageContext.getOut();
    try{
      
      out.println("<input type='hidden' name='fileNames' value='" + (Utility.isEmpty(fileNames)? "" : fileNames) + "'>");
      out.println("<input type='hidden' name='dmsFileNames' value='" + (Utility.isEmpty(dmsFileNames)? "" : dmsFileNames) + "'>");
      out.println("<input type='hidden' name='displayFileNames' value='" + (Utility.isEmpty(displayFileNames)? "" : displayFileNames) + "'>");
      out.println("<input type='hidden' name='displayDmsFileNames' value='" + (Utility.isEmpty(displayDmsFileNames)? "" : displayDmsFileNames) + "'>");
      if (!GlobalConstant.NAV_MODE_VIEW.equals(navMode)) {
    
        out.println("<div style=\"width:"+(width+153)+"px;clear:right;\">");
        out.println("<div id=\"" + this.objectName + "\" class=\"form-style-2-auto-textarea\"");
        out.println("  style=\"width:"+width+"px;height:"+height+"px;overflow:auto;background-color:#FFFFFF;layer-background-color:#FFFFFF;float:left;margin-right:10px;\">");
        out.println("</div>");
       
        //added by nancy
        out.println("<script language=\"javascript\" src=\""+this.contextPath+"/js/WorkflowAttachment.jsp\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
        if(!Utility.isEmpty(fileNames)||!Utility.isEmpty(dmsFileNames)){
          if(!Utility.isEmpty(fileNames)){
            out.println("<script language=\"javascript\">PutFileNames('"+TextUtility.escapeJSString(fileNames)+"', '"+ TextUtility.escapeJSString(displayFileNames)+"');</script>");
          }
          if(!Utility.isEmpty(dmsFileNames)){
            out.println("<script language=\"javascript\">setDmsFileNamesValueForWorkflow('"+TextUtility.escapeJSString(displayDmsFileNames)+"', '"+TextUtility.escapeJSString(dmsFileNames)+"');</script>");
          }
        }else{
          out.println("<script language=\"javascript\">redrawAttachmentPanelForWorkflow();</script>");
        }
       //end add
        out.println(TagUtil.renderStandardButton(pageContext, "upload.label.uploadattachement","return OpenCenteredPopup('" + this.contextPath + this.URL + "', '"+ this.objectName + "', '" + popW + "', '" + popH + "', 'scrollbars=yes');",null)); 
        out.println("</div>");

      } else {
      	out.println(TextUtility.noNull(displayFileNames));
      	out.println(TextUtility.noNull(displayDmsFileNames));
      }
    }catch(Exception ex){
log.debug(ex.getMessage(), ex);
    }
    return sb.toString();
  }

  protected void init() throws ApplicationException {
    sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
    this.initAttachmentList();
    if ( !validateTag() ){
      throw new IllegalArgumentException();
    }

  }

  protected void initAttachmentList(){
    if( !Utility.isEmpty(this.getAttachmentList()) ){
    	List templist = this.getAttachmentList();
			
			if(!Utility.isEmpty(templist) && !templist.equals(null)){
				StringBuffer fileNames = new StringBuffer();
				StringBuffer displayFileNames = new StringBuffer();
				StringBuffer dmsFileNames = new StringBuffer();
				StringBuffer displayDmsFileNames = new StringBuffer();
				int j = 0;
				int k = 0;
				for(int q = 0; q < templist.size(); q++){
					WorkflowProgressAttachment ff = (WorkflowProgressAttachment)templist.get(q);
					if(ff.getAttachmentType().equals(WorkflowProgressAttachment.ATTACHED_SINGLE_FILE)){
						j = j + 1;
						displayFileNames.append(ff.getAttachmentName() + ";");
						fileNames.append(ff.getAttachmentName() + ":" + ff.getExternalID() + "|");
					}else{
						k = k + 1;
						displayDmsFileNames.append(ff.getAttachmentName() + ";");
						dmsFileNames.append("[" + ff.getExternalID() + "]:[" + ff.getAttachmentName() + "]:["+ff.getContentSize() + "]"+GlobalConstant.FILENAME_SEPARATOR);
						
					}
				}
				
				this.setFileNames((new Integer(j)).toString() + "|" + fileNames.toString());
				this.setDisplayFileNames(displayFileNames.toString());            
				this.setDmsFileNames(dmsFileNames.toString()); 
				this.setDisplayDmsFileNames(displayDmsFileNames.toString());
			}       
    } 
  }

 
  
}
